import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.JTextArea;


public class Editor {
	private Analizador_Lexico LA = new Analizador_Lexico();
	  int lin=1;
	  public Editor() {
	  }

	  public void cargar(JTextArea ta,JTextArea num,JTextArea error,String path){
	    try {
	     BufferedReader br = new BufferedReader(new FileReader(path));
	     String linea = null;
	     try {
	       linea = br.readLine();
	       ta.append(linea);
	       num.append("\n "+String.valueOf(lin));
	       error.append("\n ");
	       while (linea!=null) {
	         linea = br.readLine();
	         lin++;
	         if (linea!=null){
	           ta.append("\n" + linea);
	           num.append("\n " + String.valueOf(lin));
	           error.append("\n ");
	         }
	       }
	     }
	     catch (IOException ex) {
	     }
	     try {
	       br.close();
	     } catch (IOException e) {}
	   } catch (FileNotFoundException e) {}
	  }

	  public void agregarlinea(JTextArea ta,JTextArea error){
	    ta.append("\n " +""+String.valueOf(lin));
	    error.append("\n ");
	    lin++;
	  }

	  public void guardar(JTextArea ta,String path){
	    String linea=null;
	    try {
	      BufferedWriter br = new BufferedWriter(new FileWriter(path));
	      linea = ta.getText();
	      br.write(linea, 0, linea.length());
	      br.newLine();
	      linea=null;
	      try {
	        br.close();
	      } catch (IOException e) {}
	    } catch (IOException e) {}

	  }

	  public void inic(JTextArea numeracion,JTextArea error) {
	    numeracion.append(" " + String.valueOf(lin));
	    error.append(" ");
	    lin++;
	  }

	  private void indicador(JTextArea error,int linea) {
	    String texto="";
	    for(int i=1;i<=lin;i++){
	      if (i==linea)
	        texto+="\u2588\u2588\u2588";
	      else
	        texto+=" ";
	      texto+="\n";
	    }
	    error.setText(texto);
	  }

	  public void indicar(JTextArea error,int x){
	    Error e = (Error)LA.getErrors().get(x);
	    int linea = e.getLine();
	    indicador(error,linea);
	  }

	  public void Compilar(JTextArea editor,JTable ts,JList tokens,JList e,JTextArea error) {
	    indicador(error,10000);
	    String codigo = editor.getText();
	    LA.AnalizarCodigo(codigo);
	    ModeloTabla tab = new ModeloTabla(LA.getSimbolTable());
	    ts.setModel(tab);
	    ModeloToken tok = new ModeloToken(LA.getTokens());
	    tokens.setModel(tok);
	    ModeloError err = new ModeloError(LA.getErrors());
	    e.setModel(err);
	  }
}
